package com.example.mystore.controller;

import com.example.mystore.model.ProductCategory;
import com.example.mystore.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/product-category")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;

    @GetMapping("/{id}")
    public ProductCategory getProductCategoryById(@PathVariable Integer id) {
        return productCategoryService.getProductCategoryById(id);
    }

    @GetMapping("/parent/{parentId}")
    public List<ProductCategory> findTypesByParentId(@PathVariable("parentId") Integer parentId) {
        return productCategoryService.findParamsByParentId(parentId);
    }

    @GetMapping
    public ResponseEntity<List<ProductCategory>> getAllProductCategories(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "parent_id", required = false) Integer parentId,
            @RequestParam(value = "level",required = false)Integer level,
            @RequestParam(value = "name", required = false) String name) {

            return ResponseEntity.ok(productCategoryService.getAllProductCategories());
    }

    @PostMapping("/add")
    public ProductCategory addProductCategory(@RequestBody ProductCategory productCategory) {
        return productCategoryService.addProductCategory(productCategory);
    }

    @PutMapping("/update")
    public void updateProductCategory(@RequestParam Integer id, @RequestParam String name) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(id);
        productCategory.setName(name);
        productCategoryService.updateProductCategory(id,name);
    }

    @DeleteMapping("/delete/{id}")
    public void deleteProductCategory(@PathVariable Integer id) {
        productCategoryService.deleteProductCategory(id);
    }

    @GetMapping("/has-subcategories/{id}")
    public ResponseEntity<Map<String, Boolean>> hasSubcategories(@PathVariable Integer id) {
        boolean hasSubcategories = productCategoryService.hasSubcategories(id);
        Map<String, Boolean> response = new HashMap<>();
        response.put("hasSubcategories", hasSubcategories);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/has-products/{id}")
    public ResponseEntity<Map<String, Boolean>> hasProducts(@PathVariable Integer id) {
        boolean hasProducts = productCategoryService.hasProducts(id);
        Map<String, Boolean> response = new HashMap<>();
        response.put("hasProducts", hasProducts);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/check-name")
    public ResponseEntity<Map<String, Boolean>> checkProductCategoryName(@RequestBody Map<String, String> request) {
        String name = request.get("name");
        boolean exists = productCategoryService.checkProductCategoryNameExists(name);
        Map<String, Boolean> response = new HashMap<>();
        response.put("exists", exists);
        return ResponseEntity.ok(response);
    }
}
